#include "FunctionNode.h"

namespace jcrada {

    FunctionNode::FunctionNode( int arity ) : Node( arity ),
    _type( FT_SWAP ) {

    }

    FunctionNode::~FunctionNode( ) {

    }

    FunctionNode::eFunctionType FunctionNode::getType( ) const {
        return this->_type;
    }

    void FunctionNode::setType( FunctionNode::eFunctionType type ) {
        this->_type = type;
    }

    Node* FunctionNode::getResult( ) const {
        return getChild(getArity() - 1);
    }

    FunctionNode* FunctionNode::clone( ) const {
        FunctionNode* result = new FunctionNode(getArity());
        result->setType(getType());
        Node* child = NULL;
        for (int i = 0; i < getArity(); ++i) {
            if (getChild(i) != NULL) {
                child = getChild(i)->clone();
                child->moveTo(result, i);
            }
        }
        return result;
    }

    std::string FunctionNode::toString( ) const {
        std::string result;
        switch (getType()){
            case FT_MOVE: result = "move(";
            break;
            case FT_SWAP: result = "swap(";
            break;
            default: result = "?";
        }
        for (int i = 0 ; i < getArity(); ++i){
            if (getChild(i) != NULL){
                result += " " + getChild(i)->toString();
            }else{
                result += " ?";
            }
        }
        result +=" )";
        return result;
    }
}
